/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package utilities;

import java.util.ArrayList;
import structure.DataFile;
import structure.NodeDependency;
import structure.Variable;

/**
 *
 * @author Gary
 */
public class Tree {
    public static void set() throws InterruptedException{
        ArrayList<NodeDependency> nd = WriteL1Metric.getSortedList();
        DataFile df = StringParse.data;
        
        ArrayList<Variable> end = new ArrayList<Variable>();

        System.out.println("TREE:\n");
        
        for (NodeDependency n : nd) {
            end = new ArrayList<Variable>();
            for (Variable v : df.getVariables()) {
                if (n.getFirst() == v) {
                    Variable temp = n.getSecond();
                    if(v.hasParent(temp)){
                        if (!end.contains(v)){
                            end.add(v);
                        }
                    }
                    else if(v.hasChild(temp)){
                        if (!end.contains(v)){
                            end.add(v);
                        }
                    }
                    else{
                        temp.addParent(v);
                        end.add(temp);
                        v.addChild(temp);
                        end.add(v);
                    }
                } else if (n.getSecond() == v) {
                    Variable temp = n.getFirst();
                    if (v.hasParent(temp)) {
                        if (!end.contains(v)){
                            end.add(v);
                        }
                    } else if (v.hasChild(temp)) {
                        if (!end.contains(v)){
                            end.add(v);
                        }
                    } else {
                        temp.addParent(v);
                        end.add(temp);
                        v.addChild(temp);
                        end.add(v);
                    }
                }
                else {
                    if (!end.contains(v)){
                        end.add(v);
                    }
                }
            }
            df.setVariables(end);
            System.out.println(df.getVariables()+" argh ");
        }

        

        for(Variable temp : df.getVariables()){
            System.out.println("\nVariable " + temp.getId() + ":");
            System.out.println("--------------------------------\n");
            System.out.println("Parents:");
            for(Variable t2 : temp.getParents()){
                System.out.println("Variable " + t2.getId() + ":");
            }
            System.out.println("\nChildren:");
            for(Variable t2 : temp.getChildren()){
                System.out.println("Variable " + t2.getId() + ":");
            }
        }
    }
}
